Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
babel-plugin-transform-es2015-computed-properties
Advanced tools
Package description
The babel-plugin-transform-es2015-computed-properties npm package is a plugin for Babel that allows the transformation of computed property names in objects from ES2015/ES6 syntax to a compatible ES5 format. This is particularly useful for developers targeting environments that do not fully support ES2015 syntax, ensuring that codebases using computed properties remain compatible and functional.
Transformation of computed properties
This feature allows the transformation of ES2015 computed property names into a format that can be understood by older JavaScript engines. It converts object literals with computed properties using square bracket notation into a series of statements that define each property explicitly.
{
const property = 'foo';
const obj = {
[property]: 'bar'
};
// Transforms to:
// var obj = {};
// obj[property] = 'bar';
}
This package is a part of the Babel ecosystem and serves a similar purpose as babel-plugin-transform-es2015-computed-properties, transforming computed property names in objects. It is more up-to-date and maintained as part of the broader Babel 7 release, making it a more suitable choice for projects that are using the latest versions of Babel.
While not identical in functionality, this plugin transforms member expressions to be compatible with ES3, similar to how babel-plugin-transform-es2015-computed-properties makes computed properties compatible with ES5. It focuses on ensuring that property names that are reserved words in ES3 are quoted, which is somewhat analogous to handling computed properties.
Readme
Compile ES2015 computed properties to ES5
In
var obj = {
["x" + foo]: "heh",
["y" + bar]: "noo",
foo: "foo",
bar: "bar"
};
Out
var _obj;
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
var obj = (
_obj = {},
_defineProperty(_obj, "x" + foo, "heh"),
_defineProperty(_obj, "y" + bar, "noo"),
_defineProperty(_obj, "foo", "foo"),
_defineProperty(_obj, "bar", "bar"),
_obj
);
npm install --save-dev babel-plugin-transform-es2015-computed-properties
.babelrc
(Recommended).babelrc
Without options:
{
"plugins": ["transform-es2015-computed-properties"]
}
With options:
{
"plugins": [
["transform-es2015-computed-properties", {
"loose": true
}]
]
}
babel --plugins transform-es2015-computed-properties script.js
require("babel-core").transform("code", {
plugins: ["transform-es2015-computed-properties"]
});
loose
boolean
, defaults to false
Just like method assignment in classes, in loose mode, computed property names use simple assignments instead of being defined. This is unlikely to be an issue in production code.
In
var obj = {
["x" + foo]: "heh",
["y" + bar]: "noo",
foo: "foo",
bar: "bar"
};
Out
var _obj;
var obj = (
_obj = {},
_obj["x" + foo] = "heh",
_obj["y" + bar] = "noo",
_obj.foo = "foo",
_obj.bar = "bar",
_obj
);
FAQs
Compile ES2015 computed properties to ES5
The npm package babel-plugin-transform-es2015-computed-properties receives a total of 1,060,744 weekly downloads. As such, babel-plugin-transform-es2015-computed-properties popularity was classified as popular.
We found that babel-plugin-transform-es2015-computed-properties demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.